<?php
include_once '../include/inc_session.php';

	$sEdition =$_GET['edition'];
	$sDateDebut =$_GET['dd'];
	$sDateFin =$_GET['df'];
	
	$oElementDico = new dataObject_elementdico();
	$oEditions 	  = new dataObject_editions();
	
	$sNomModule = $oBloc->_MENU_NOM;
	$sNomListe;
	$bEnvoiMail = false;

	//on envoi le mail si click sur le bouton envoi mail
	if($gbFormValid && isset($_POST['Envoyer']))
	{
		//$sMsgWait = "Veuillez patienter pendant l'envoi du mail... ";
		include_once 'succes_envoi_mail.php';
	}
    if(isset($_POST['Retour'])) header('location:index.php');
    
    
    
    $idUser = $_SESSION['iduser'];
	$oUser = new dataObject_utilisateur($idUser);
	$oFct = new dataObject_fonction($oUser->_IDENTIFIANT_FONCTION);
	
	switch($oFct->_CODE_FONCTION)
	{						
			case dataObject_fonction::_FCT_DIR_REGION:
				//directeur de région
				$bShowTotalGeneral = false;	
				break;
	
			case dataObject_fonction::_FCT_DIR_SIEGE:
				//directeur de siege
				$bShowTotalGeneral = true;	
				break;		
	}
    
	$aEnteteTab = array(
	array("num_colonne"=>1, "nom_colonne"=>"Contrat"),
	array("num_colonne"=>2, "nom_colonne"=>"Date effet"),
	array("num_colonne"=>3, "nom_colonne"=>"Offre"),
	array("num_colonne"=>4, "nom_colonne"=>"Produit"),
	array("num_colonne"=>5, "nom_colonne"=>"Formule")
	);

switch ($sEdition)
{
	case '134':
		{
			header("Location:echeances.php?edition=$sEdition&dd=$sDateDebut&df=$sDateFin");
			break;
		}
	case '135':
		{
			$sNomListe = $oElementDico->elementdico_getNomFromCode(AG,dataObject_dictionnaire::LISTES_EDITION);
			$aEnteteTab = array_merge($aEnteteTab,array(array("num_colonne"=>6, "nom_colonne"=>"Acte de gestion")
			));
			$sTabActe = "
				<tr id=\"niv1\">
					<td class='col3'  style=' width:34%; height:25px;'>&nbsp;</td>
					<td class='col3'  style=' width:32%; height:25px; font-weight:bold;' align='center'>Acte de gestion</td>
					<td class='col3'  style=' width:32%; height:25px; font-weight:bold;' align='center'>Nombre d'actes</td>
				</tr>";
			$aPdfWidthRecord_135 = array("Contrat" => 16,
										   "Date effet" => 12,
						"Offre" => 14,
						"Produit" => 14,
						"Formule" => 20,
						"Acte de gestion" => 22
						);
			$aActe = $aActeUnite = $aActeRegion = $aActeGeneral = array();
			break;
		}
	case '136':
		{
			header("Location:echeances.php?edition=$sEdition&dd=$sDateDebut&df=$sDateFin");
			break;
		}
	case '137':
		{
			$sNomListe = $oElementDico->elementdico_getNomFromCode('SID', dataObject_dictionnaire::LISTES_EDITION);
			$aEnteteTab = array_merge($aEnteteTab, array( array("num_colonne"=>6, "nom_colonne"=>"Type"),
					array("num_colonne"=>7, "nom_colonne"=>"Date <br/>survenance&nbsp;d&eacute;claration"),
					array("num_colonne"=>9, "nom_colonne"=>"Etat"),
					array("num_colonne"=>10, "nom_colonne"=>"D&eacute;cision"),
					array("num_colonne"=>11, "nom_colonne"=>"Motif refus")
			));
			$sTabSinistreDec = "
				<tr id=\"niv1\">
					<td class='col3' style=' width:34%; height:25px;'>&nbsp;</td>
					<td class='col3' align='center'  style=' width:32%; height:25px; font-weight:bold; '>Type de sinistre</td>
					<td class='col3' align='center'  style=' width:32%; height:25px; font-weight:bold;'>Nombre de sinistres</td>
				</tr>";
			$aPdfWidthRecord_137 = array("Contrat" => 12,
										   "Date effet" => 8,
						"Offre" => 10,
						"Produit" => 10,
						"Formule" => 14,
						"Type" => 7,
						"Date <br/>survenance&nbsp;déclaration" => 14,
						"Etat" => 7,
						"D&eacute;cision" => 7,
						"Motif refus" => 9
						);
			$aSinistreDec = $aSinistreDecUnite = $aSinistreDecRegion = $aSinistreDecGeneral = array();
			break;
		}
	case '138':
		{
			$sNomListe = $oElementDico->elementdico_getNomFromCode(SIR,dataObject_dictionnaire::LISTES_EDITION);
			$aEnteteTab = array_merge($aEnteteTab,array(array("num_colonne"=>6, "nom_colonne"=>"Type"),
			array("num_colonne"=>7, "nom_colonne"=>"Date <br/>survenance&nbsp;d&eacute;claration"),
			/*array("num_colonne"=>8, "nom_colonne"=>"Date déclaration"),*/
			array("num_colonne"=>9, "nom_colonne"=>"Type prestation"),
			array("num_colonne"=>10, "nom_colonne"=>"Montant"),
			array("num_colonne"=>11, "nom_colonne"=>"Date r&eacute;glement")
			));
			$sTabSinistreReg = "
				<tr id=\"niv1\">
					<td class='col1'  style=' width:18%;'>&nbsp;</td>
					<td class='col2' align='center'  style=' width:20%; font-weight:bold;'>Type de sinistre</td>
					<td class='col3' align='center'  style=' width:21%; font-weight:bold;'>Nombre sinistres</td>
					<td class='col4' align='center'  style=' width:22%; font-weight:bold;'>Type de prestation</td>
					<td class='col6' nowrap='nowrap' align='center'  style=' width:17%; font-weight:bold;'>Montant prestations</td>
				</tr>";
				
			$aPdfWidthRecord_138 = array("Contrat" => 12,
										   "Date effet" => 7,
						"Offre" => 10,
						"Produit" => 10,
						"Formule" => 14,
						"Type" => 7,
						"Date <br/>survenance&nbsp;déclaration" => 14,
						"Type prestation" => 8,
						"Montant" => 7,
						"Date r&eacute;glement" => 9
						);
			$aSinistreReg = $aSinistreRegUnite = $aSinistreRegRegion = $aSinistreRegGeneral = array();//pour l'affichage si pas d'elements
			$aPrestation = $aPrestationUnite = $aPrestationRegion = array();
			break;
		}
	case '139':
		{
			$sNomListe = $oElementDico->elementdico_getNomFromCode(SO,dataObject_dictionnaire::LISTES_EDITION);
			$aEnteteTab = array_merge($aEnteteTab,array(
			array("num_colonne"=>6, "nom_colonne"=>"Cotisation"),
			array("num_colonne"=>7, "nom_colonne"=>"Droit entr&eacute;e"),
			array("num_colonne"=>8, "nom_colonne"=>"Fr&eacute;quence")));
			$sTabSous = "
					<tr id='niv1'>
						<td  class='col3' style='text-align:center;  width:24%; font-weight:bold;'>&nbsp;</td>
						<td  class='col3' style='text-align:center;  width:26%; font-weight:bold;'>Nombre Contrats</td>
						<td  class='col3' style='text-align:center;  width:28%; font-weight:bold;'>Montant Cotisations</td>
						<td  class='col3' style='text-align:center;  width:20%; font-weight:bold;'>Montant Droits entr&eacute;e</td>
					</tr>";
		
			$aPdfWidthRecord_139 = array("Contrat" => 14,
										   "Date effet" => 10,
						"Offre" => 12,
						"Produit" => 14,
						"Formule" => 20,
						"Cotisation" => 9,
						"Droit entr&eacute;e" => 9,
						"Fr&eacute;quence" => 10
						);
			$fTotalCotisation = $iNbContrat = $fTotalDE = 0;
			break;
		}
}

$sSepareTable = '</table><div id="br2"></div>
				 <table class="tab_list" border="1" style=" border:1px solid black; border-collapse:collapse;">';

$sSepareTableVide = '<div id="br2"></div>
				 <table class="tab_list" border="1" style=" border:1px solid black; border-collapse:collapse;">'; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Editions</title>
			<link rel="stylesheet" type="text/css" href="../css/main_editions.css" />
            <link rel="stylesheet" type="text/css" href="../css/list_editions.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
            <script> 
			function printEdition(urlImpression)
			    {
			    	options = "Width=850,Height=670" ;
			    	window.open(urlImpression,"editions_region", options) ;
			    }
			</script>
	</head>
	<body>
		<div id="sf_menu1">
			<h3><?php echo $sNomModule ?></h3>
			<div id="information_erreur">
			<?php 
				if(isset($sMsgWait)) echo colorer($sMsgWait, "false");
				$aMessages = $oForm->getValidMessage();
				if(!empty($aMessages))
				{
			?>
				<ul id="message">
					<li><?php echo $aMessages[0]['message'] ?></li>
				</ul>
			<?php } ?>
			</div>
		</div><!-- fin sf_menu1 -->
		
		<div class="cleared"></div>
		
		<div class="corps1">
			
		<!--/***************************************/-->
			<div class="imgleft1"></div>
			
				
			<div class="imgmiddle1">
			
				<div class="btn">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>Demande d'&eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
				<div class="btn1">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>D&eacute;tail &eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
		        <PDF>
				<table align="center">
					<tr>
						<td align="center"><font size="2"><?php echo $sNomListe.'	du	'.formaterDate($sDateDebut,"-","/",false).'	au	'.formaterDate($sDateFin,"-","/",false)?></font></td>
					</tr>
				</table>
				<div id="br2"></div>
				<div class="content_table bright">
<?php
if (!$bEnvoiMail)
{
	$iTotalGeneral = $iMontPrest = $fCotisation = $fDroit = 0;
	$iMontPrestUnite = $fCotisationUnite = $fDroitUnite = 0;
	$iMontPrestRegion = $fCotisationRegion = $fDroitRegion = 0;
	
	if($bShowTotalGeneral) // directeur siege => on liste toutes les regions
		$aListeRegion = $oEditions->regionsDirection(dataObject_editions::getIdDirection());
	else // directeur region => sa region seulement
		$aListeRegion = $oEditions->infosRegion($oEditions->idRegion($idUser));

	$iNbTableHtml = 0;
	$idRegionPrec = 0;
	foreach ($aListeRegion as $iReg => $aRegion)
	{
		$aListesUnites = $oEditions->unitesRegion($aRegion['Identifiant_region']);
		if($iReg>0) 
		{
			echo "<p>&nbsp;</p>";
			$sSpaceRegion = "<tr><td>&nbsp;</td></tr>";
		}
		if(($idRegionPrec != intval($aRegion['Identifiant_region'])) )
		{
?>
	<table style="width:400px; text-align: left;" >
		<?php echo $sSpaceRegion ?>
		<tr align="left" >
				<td style="width:20%; text-align: left;">R&eacute;gion : </td>
				<td style="width:25%; text-align: left;"><?php echo $aRegion['Code_region']?></td>
				<td style="width:55%; text-align: left;"><?php echo $aRegion['Nom_region']?></td>
		</tr>
	</table>
<?php
		}
		$idRegionPrec = intval($aRegion['Identifiant_region']);
		foreach ($aListesUnites as $iUnit => $aUnite)
		{
			$aListesAgents = $oEditions->agentsUnite($aUnite['Identifiant_unite']);
			//on considere aussi l'agent 'NULL' pour les traitements différés
			$aListesAgents[] = array('Identifiant_agent' => '0', 
									 'Code_agent'=> '', 
									 'Nom_agent'=> '' ,
									 'Identifiant_unite'=> $aUnite['Identifiant_unite']);
			
			//on ne recupere que les agents qui ont enregistre un sinitre dans cet edition
			if($sEdition == '137')
			{
				$aListesAgents = $oEditions->getAgentWithSinDeclareUnite($aUnite['Identifiant_unite'], $sDateDebut, $sDateFin);
			}
			if($iUnit>0) 
			{
				echo "<p>&nbsp;</p>";
				$sSpaceUnite = "<tr><td>&nbsp;</td></tr>";
			}
?>
	
	<table  style="width:400px; text-align: left">
	<?php echo $sSpaceUnite ?>
		<tr>
				<td style="width:20%; text-align: left;">Unit&eacute;&nbsp;&nbsp;&nbsp;:</td>
				<td style="width:25%; text-align: left;"><?php echo $aUnite['Code_unite']?></td>
				<td style="width:55%; text-align: left;" nowrap="nowrap"><?php echo $aUnite['Nom_unite'] ?></td>
		</tr>
	</table>
<?php		
			if(empty($aListesAgents))
			{
				echo '<div style="width: 98%;"><hr style="margin-right:17px; width:818px;" /></div>';
			}
			$idAgentPrec = 0;
			foreach ($aListesAgents as $iAg => $aAgent)
			{
				$iNbTableHtml++;
				$sSpaceAgent = ''; $sSpacePDF = '';
				if($iAg>0) 
				{
					$sSpacePDF = "<p></p>";
					$sSpaceAgent = "<tr><td>&nbsp;</td></tr>";
				}
				$idAgentPrec = intval($aAgent['Identifiant_agent']);
					
				$aListes = array();
				$bShowEntDetail = true;
				switch ($sEdition)
				{
					case '135':
							$aListes = $oEditions->listeActesDeGestionAgent($aAgent['Identifiant_agent'],$sDateDebut,$sDateFin, $aAgent['Identifiant_unite']);
							if(empty($aListes)) $bShowEntDetail = false;
							break;
						
					case '137':
							$aListes = $oEditions->listeSinistresDeclaresAgent($aAgent['Identifiant_agent'],$sDateDebut,$sDateFin);
							break;
							
					case '138':
							$aListes = $oEditions->listeSinistresReglesAgent($aAgent['Identifiant_agent'],$sDateDebut,$sDateFin);
							if(empty($aListes)) $bShowEntDetail = false;
							break;
					
					case '139':
							$aListes = $oEditions->listeSouscriptionsAgent($aAgent['Identifiant_agent'],$sDateDebut,$sDateFin);
							break;
				}
				
?>
	<?php echo $sSpacePDF; ?>
	<table style="width:400px; text-align: left">
		<?php echo $sSpaceAgent; ?>
		<tr>
			<td  style="width:20%; text-align: left;">Agent&nbsp;&nbsp;: </td>
			<td  style="width:25%; text-align: left;"><?php echo $aAgent['Code_agent'] ?></td>
			<td  style="width:55%; text-align: left;"><?php echo $aAgent['Nom_agent'] ?></td>
		</tr>
		
	</table>
	<div style="width: 98%; display: block;"><hr style="margin-right:17px; width:818px;" /></div>
	
	<table class="tab_list"  style=" border:1px solid black; border-collapse:collapse;" border="1">
	
	<?php 
		//debut: on n'affiche pas l'entete du tableau detail si l'agent n'a pas d'acte de gestion	
		if( $bShowEntDetail )
		{
	?>
			<tr id="niv1">
			<?php
				$i = 1;
				foreach ($aEnteteTab as $iKey => $aEntete)
				{
					$sClass = "col".$i;
					$sClassOther = '';
					if($aEntete['nom_colonne']=='Type') $sClass = "col2";
					if($i==7) $sClass = "col6";
					if($aEntete['nom_colonne']== 'Date effet') $sClass = "col1";
					if($aEntete['nom_colonne']== 'Cotisation') $sClass = "col2";
					if($aEntete['nom_colonne']== "Droit entr&eacute;e") $sClass = "col3";
					if($aEntete['nom_colonne']== "Fr&eacute;quence") $sClass = "col3";
					if(($sEdition == '138')) 
					{
						if($aEntete['nom_colonne']== 'Date effet')
							$sClassOther = "nowrap='nowrap'";
						if($aEntete['nom_colonne']== 'Date r&eacute;glement')
							$sClass = "col15";
						if($aEntete['nom_colonne']== 'Offre')
							$sClass = "col15";
					}
					
				?>
				<td class='<?php echo $sClass ?>' <?php echo $sClassOther ?> style='text-align:center; height:25px; width:<?php echo ${'aPdfWidthRecord_'.$sEdition}[$aEntete['nom_colonne']]?>%; font-weight:bold;  '><?php echo $aEntete['nom_colonne']?></td>
				<?php
					$i++;
				} 
				?>
			</tr>

		<?php
		}
		//fin: on n'affiche pas l'entete du tableau detail si l'agent n'a pas d'acte de gestion	
		
		$fTotalCotisation = $fTotalDD = $fTotalPrestation = 0;
		$fTotalCotisationUnite = $fTotalDDUnite = $fTotalPrestationUnite = 0;
		$fTotalCotisationRegion = $fTotalDDRegion = $fTotalPrestationRegion = 0;
		$fTotalCotisationGeneral = $fTotalDDGeneral = $fTotalPrestationGeneral = 0;
		
		foreach ($aListes as $iKey => $aElement)
		{
		?>
			<tr id='niv2'>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Contrat']?>%; text-align:center; height:25px;" ><?php echo $aElement['Numero_contrat']?></td>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date effet']?>%; height:25px;" ><?php echo formaterDate($aElement['Date_effet_contrat'])?></td>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Offre']?>%; height:25px;"><?php echo htmlentities($aElement['Libelle_offre'], ENT_COMPAT,'utf-8');?></td>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Produit']?>%; height:25px;"><?php echo htmlentities($aElement['Libelle_produit'], ENT_COMPAT,'utf-8');?></td>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Formule']?>%; height:25px;" ><?php echo htmlentities($aElement['Libelle_formule'], ENT_COMPAT,'utf-8');?></td>
	
				<?php if (isset($aElement['Montant_base_cotisation']))
				{
					// SOUSCRIPTIONS
					$fTotalCotisation += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
					$iNbContrat++; 
					$fTotalDE += $aElement['Montant_droits_entree_cotisation'];
					
					//pour l'unite
					$iNbContratUnite++; 
					$fTotalCotisationUnite += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
					$fTotalDEUnite += $aElement['Montant_droits_entree_cotisation'];
					
					//pour la region
					$iNbContratRegion++;
					$fTotalCotisationRegion += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
					$fTotalDERegion += $aElement['Montant_droits_entree_cotisation'];
					
					//pour le siege
					$iNbContratGeneral++;
					$fTotalCotisationGeneral += $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation'];
					$fTotalDEGeneral += $aElement['Montant_droits_entree_cotisation'];
					$iTotalGeneral++;
				?>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Cotisation']?>%; height:25px;"><?php echo $aElement['Montant_base_cotisation']+$aElement['Montant_frais_echeance_cotisation']?></td>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Droit entr&eacute;e']?>%; height:25px;"><?php echo $aElement['Montant_droits_entree_cotisation']?></td>
				<?php 
				} ?>
	
	
				<?php
				$nbRowSpan = 0; 
				if (isset($aElement['Nom_element']))
				{
					// ACTES DE GESTION
					if (!isset($aActe[$aElement['Nom_element']]))
					{
						$aActe[$aElement['Nom_element']] = 1;
						if (!isset($aActeUnite[$aElement['Nom_element']]))
							$aActeUnite[$aElement['Nom_element']] = 1;
						else $aActeUnite[$aElement['Nom_element']]++;
							
						if (!isset($aActeRegion[$aElement['Nom_element']]))
							$aActeRegion[$aElement['Nom_element']] = 1;
						else $aActeRegion[$aElement['Nom_element']]++;
						
						if (!isset($aActeGeneral[$aElement['Nom_element']]))
							$aActeGeneral[$aElement['Nom_element']] = 1;
						else $aActeGeneral[$aElement['Nom_element']]++;
					}
					else
					{
						$aActe[$aElement['Nom_element']]++;
						$aActeUnite[$aElement['Nom_element']]++;
						$aActeRegion[$aElement['Nom_element']]++;
						$aActeGeneral[$aElement['Nom_element']]++;
					}
					$nbRowSpan = count($aActe); 
					?>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Acte de gestion']?>%; height:25px; text-align:center;"><?php echo $aElement['Nom_element']?></td>
				<?php 
				} ?>

				<?php 
				if (isset($aElement['Frequence_paiement']))
				{ ?>
				<td  align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Fr&eacute;quence']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Frequence_paiement'],'M0007') ?></td>
				<?php 
				} ?>
	
				<?php 
				if (isset($aElement['Type_sinistre']))
				{
					// SINISTRES
					?>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Type']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Type_sinistre'], dataObject_dictionnaire::TYPE_SINISTRE)?></td>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date <br/>survenance&nbsp;déclaration']?>%; height:25px;"><?php echo formaterDate($aElement['Date_survenance_sinistre'])."&nbsp;&nbsp;".formaterDate($aElement['Date_declaration_sinistre'])?></td>
				<?php 
				} ?>
	
				<?php 
				
				if (isset($aElement['Type_prestation']))
				{
					
					// SINISTRES REGLES
					if (!isset($aSinistreReg[$aElement['Type_sinistre']]))
					{
						$aSinistreReg[$aElement['Type_sinistre']] = 1;
						$aSinistreRegUnite[$aElement['Type_sinistre']] = 1;
						$aSinistreRegRegion[$aElement['Type_sinistre']] = 1;
						$aSinistreRegGeneral[$aElement['Type_sinistre']] = 1;
					}
					else
					{
						$aSinistreReg[$aElement['Type_sinistre']]++;
						$aSinistreRegUnite[$aElement['Type_sinistre']]++;
						$aSinistreRegRegion[$aElement['Type_sinistre']]++;
						$aSinistreRegGeneral[$aElement['Type_sinistre']]++;
					}
					
					if (!isset($aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']]))
					{
						$aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
						$aPrestationUnite[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
						$aPrestationRegion[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
						$aPrestationGeneral[$aElement['Type_sinistre']][$aElement['Type_prestation']] = $aElement['Montant_prestation'];
					}
					else
					{
						$aPrestation[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
						$aPrestationUnite[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
						$aPrestationRegion[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
						$aPrestationGeneral[$aElement['Type_sinistre']][$aElement['Type_prestation']] += $aElement['Montant_prestation'];
					}
				
					$nbRowSpan = count($aSinistreReg); 
					$fTotalPrestation += $aElement['Montant_prestation']; //utilisé?
					?>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Type prestation']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode('N'.$aElement['Type_prestation'], dataObject_dictionnaire::TYPE_PRESTATION)?></td>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Montant']?>%; height:25px;"><?php echo $aElement['Montant_prestation']?></td>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Date r&eacute;glement']?>%; height:25px;"><?php echo formaterDate($aElement['Date_evenement'])?></td>
				<?php 
				} ?>
	
				<?php if (isset($aElement['Etat_sinistre']))
				{
					// SINISTRES DECLARES
					if (!isset($aSinistreDec[$aElement['Type_sinistre']]))
					{
						$aSinistreDec[$aElement['Type_sinistre']] = 1;
						$aSinistreDecUnite[$aElement['Type_sinistre']] = 1;
						$aSinistreDecRegion[$aElement['Type_sinistre']] = 1;
						$aSinistreDecGeneral[$aElement['Type_sinistre']] = 1;
					}
					else
					{
						$aSinistreDec[$aElement['Type_sinistre']]++;
						$aSinistreDecUnite[$aElement['Type_sinistre']]++;
						$aSinistreDecRegion[$aElement['Type_sinistre']]++;
						$aSinistreDecGeneral[$aElement['Type_sinistre']]++;
					}
					$nbRowSpan = count($aSinistreDec); 
					?>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Etat']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Etat_sinistre'],dataObject_dictionnaire::_ETAT_SINISTRE)?></td>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['D&eacute;cision']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Decision_partenaire_sinistre'], dataObject_dictionnaire::_TYPE_DECISION_ASSUREUR)?></td>
				<td align='center' style=" width:<?php echo ${'aPdfWidthRecord_'.$sEdition}['Motif refus']?>%; height:25px;"><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Motif_refus_sinistre'], dataObject_dictionnaire::MOTIF_REFUS_INDEMNISATION)?></td>
				<?php 
				} ?>
			</tr>
			<?php 
			}
			
			if (isset($aActe))
			{
				
				$sTotalAgent = '';
				foreach ($aActe as $iKey => $sVal)
				{
					$sTotalAgent .= "
					<tr id='niv2'>
						<td class='col3' style=' width:34%; '>Agent ".$aAgent['Nom_agent']."</td>
						<td class='col3' style=' width:32%; ' align='center'>".$iKey."</td>
						<td class='col3' style=' width:32%; '  align='center'>".$sVal."</td>
					</tr>";
				}
				if($sTotalAgent=='')
					$sTotalAgent = "
					<tr id='niv2'>
						<td class='col3' style=' width:34%; '>Agent ".$aAgent['Nom_agent']."</td>
						<td class='col3' style=' width:32%; ' align='center'> - </td>
						<td class='col3' style=' width:32%; ' align='center'>0</td>
					</tr>";
				
				$aActe = array();
				echo $sSepareTable.$sTabActe;
			}
			else if (isset($aSinistreDec))
			{
				$sTotalAgent = '';
				foreach ($aSinistreDec as $iKey => $sVal)
				{
					$sTotalAgent .= "
					<tr>
						<td class='col3'  style=' width:34%; '>Agent ".$aAgent['Nom_agent']."</td>
						<td class='col3'  style=' width:32%; ' align='center' >".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
						<td class='col3' style=' width:32%; ' align='center' >".$sVal."</td>
					</tr>";
				}
				
				$aSinistreDec = array();
				echo  $sSepareTable.$sTabSinistreDec;
			}
			else if (isset($aSinistreReg))
			{
				
				$sTotalAgent = '';
				foreach ($aSinistreReg as $iKey => $sVal)
				{
					foreach ($aPrestation[$iKey] as $iKey1 => $sVal1)
					{
						$sTotalAgent .= "
						<tr>
							<td class='col1'  style=' width:18%;'>Agent ".$aAgent['Nom_agent']."</td>
							<td class='col2' align='center'  style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
							<td class='col2' align='center'  style=' width:21%;'>".$sVal."</td>
							<td class='col2' align='center'  style=' width:22%;'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1, dataObject_dictionnaire::TYPE_PRESTATION)."</td>
							<td class='col2' align='center'  style=' width:17%;'>".$sVal1."</td>
						</tr>";
						$iMontPrest += $sVal1;
					} 
				}
				if($sTotalAgent=='')
					$sTotalAgent = "
						<tr>
							<td class='col1'  style=' width:18%;'>Agent ".$aAgent['Nom_agent']."</td>
							<td class='col2' rowspan='1' align='center'  style=' width:20%;'> - </td>
							<td class='col2' align='center'  style=' width:21%;'>0 </td>
							<td class='col2' rowspan='1' align='center'  style=' width:22%;'> - </td>
							<td class='col2' align='center'  style=' width:17%;'>0 </td>
						</tr>";
				$aSinistreReg = array();
				$aPrestation = array();
				echo  $sSepareTable.$sTabSinistreReg;
			}
			else
			{
				$sTotalAgent = "
				<tr id='niv2'>
					<td  class='col3' style=' width:24%;'>Agent ".$aAgent['Nom_agent']."</td>
					<td  class='col3' align='center' style=' width:26%;'>".$iNbContrat."</td>
					<td  class='col3' align='center' style=' width:28%;'>".$fTotalCotisation."</td>
					<td  class='col3' align='center' style=' width:20%;'>".$fTotalDE."</td>
				</tr>";
				
				$fCotisation += $fTotalCotisation;
				$fDroit += $fTotalDE;
				//pour l'unite
				$fCotisationUnite += $fTotalCotisation;
				$fDroitUnite += $fTotalDE;
				//pour la region
				$fCotisationRegion += $fTotalCotisation;
				$fDroitRegion += $fTotalDE;
				//pour le siege 
				$fCotisationGeneral += $fTotalCotisation;
				$fDroitGeneral += $fTotalDE;
				
				//reinitialisation
				$fTotalCotisation = $iNbContrat = $fTotalDE = 0;
				echo  $sSepareTable.$sTabSous;
			}
			echo  $sTotalAgent;
			
		
		} //fin foreach agents de l'unite
		
			//apres avoir parcouru les agents de l'unité, on affiche le total de l'unité
			if (isset($sTabActe) && $sTabActe != "")
			{
				$sTotalUnite = '';
				foreach ($aActeUnite as $iKey => $sVal)
				{
					$sTotalUnite .= "
					<tr id='niv2'>
					<td class='col3' style=' width:34%; '>Unit&eacute; ".$aUnite['Nom_unite']."</td>
					<td class='col3' style=' width:32%; ' align='center'>".$iKey."</td>
					<td class='col3' style=' width:32%; ' align='center'>".$sVal."</td>
					</tr>";
				}
				if($sTotalUnite == '')
					$sTotalUnite = "
					<tr id='niv2'>
						<td class='col3' style=' width:34%; '>Unit&eacute; ".$aUnite['Nom_unite']."</td>
						<td class='col3' style=' width:32%; ' align='center'>-</td>
						<td class='col3' style=' width:32%; ' align='center'>0</td>
					</tr>";
				$aActeUnite = array();
				echo $sSepareTable.$sTabActe;
			}
			else if (isset($sTabSinistreDec) && $sTabSinistreDec != "")
			{
				$sTotalUnite = '';
				
				foreach ($aSinistreDecUnite as $iKey => $sVal)
				{
					$sTotalUnite .= "
					<tr id='niv2'>
						<td class='col3' style=' width:34%; '>Unit&eacute; ".$aUnite['Nom_unite']."</td>
						<td class='col3' align='center'  style=' width:32%; '>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
						<td class='col3' align='center'  style=' width:32%; '>".$sVal."</td>
					</tr>";
				}
				$aSinistreDecUnite = array();
				if($sTotalUnite == '')
					$sTotalUnite .= "
					<tr id='niv2'>
						<td class='col3' style=' width:34%; '>Unit&eacute; ".$aUnite['Nom_unite']."</td>
						<td class='col3' align='center'  style=' width:32%; '>-</td>
						<td class='col3' align='center'  style=' width:32%; '>0</td>
					</tr>";
				if(empty($aListesAgents)) echo $sSepareTableVide.$sTabSinistreDec;
				else echo $sSepareTable.$sTabSinistreDec;
				
			}
			else if (isset($sTabSinistreReg) && $sTabSinistreReg != "")
			{
				$sTotalUnite = '';
				foreach ($aSinistreRegUnite as $iKey => $sVal)
				{
					foreach ($aPrestationUnite[$iKey] as $iKey1 => $sVal1)
					{
						$sTotalUnite .= "
						<tr id='niv2'>
							<td class='col1' style=' width:18%;'>Unit&eacute; ".$aUnite['Nom_unite']."</td>
							<td class='col2' align='center'  style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
							<td class='col2' align='center'  style=' width:21%;'>".$sVal."</td>
							<td class='col2' align='center'  style=' width:22%;'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1, dataObject_dictionnaire::TYPE_PRESTATION)."</td>
							<td class='col2' align='center'  style=' width:17%;'>".$sVal1."</td>
						</tr>";
						$iMontPrestUnite += $sVal1;
					}
				}
				if($sTotalUnite == '')
					$sTotalUnite .= "
						<tr id='niv2'>
							<td class='col1' style=' width:18%;'>Unit&eacute; ".$aUnite['Nom_unite']."</td>
							<td class='col2' align='center'  style=' width:20%;'>-</td>
							<td class='col2' align='center'  style=' width:21%;'>0</td>
							<td class='col2' align='center'  style=' width:22%;'>-</td>
							<td class='col2' align='center'  style=' width:17%;'>0</td>
						</tr>";
				$aSinistreRegUnite = array();
				$aPrestationUnite = array();
				echo $sSepareTable.$sTabSinistreReg;
			}
			else
			{
				$sTotalUnite= "	<tr id='niv2'>
				<td  class='col1' style=' width:24%;'> Unit&eacute; ".$aUnite['Nom_unite']."</td>
				<td  class='col2' align='center' style=' width:26%;'>".$iNbContratUnite."</td>
				<td  class='col2' align='center' style=' width:28%;'>".$fCotisationUnite."</td>
				<td  class='col2' align='center' style=' width:20%;'>".$fDroitUnite."</td>
				</tr>";
				$fTotalCotisationUnite = $iNbContratUnite = $fTotalDEUnite = 0;
				$fCotisationUnite = $fDroitUnite = 0;
				echo $sSepareTable.$sTabSous;
			}
			echo $sTotalUnite;
			if(empty($aListesAgents)) echo '</table>';
			
			
	}//fin foreach unité de la region
	
	//apres avoir parcouru les unités de la région, on affiche le total de la région
	if (isset($sTabActe) && $sTabActe != "")
	{
		$sTotalRegion = '';
		
		foreach ($aActeRegion as $iKey => $sVal)
		{
			$sTotalRegion .= "
			<tr id='niv2'>
				<td class='col3' nowrap='nowrap' style=' width:34%; '>R&eacute;gion ".$aRegion['Nom_region']."</td>
				<td class='col3' style=' width:32%; ' align='center'>".$iKey."</td>
				<td class='col3' style=' width:32%; ' align='center'>".$sVal."</td>
			</tr>";
		}
		if($sTotalRegion == '')
			$sTotalRegion .= "
			<tr id='niv2'>
				<td class='col3' nowrap='nowrap' style=' width:34%; '>R&eacute;gion ".$aRegion['Nom_region']."</td>
				<td class='col3' style=' width:32%; ' align='center'>-</td>
				<td class='col3' style=' width:32%; ' align='center'>0</td>
			</tr>";
		$aActeRegion = array();
		echo $sSepareTable.$sTabActe;
	}
	else if (isset($sTabSinistreDec) && $sTabSinistreDec != "")
	{
		$sTotalRegion = '';
		foreach ($aSinistreDecRegion as $iKey => $sVal)
		{
			$sTotalRegion .= "<tr id='niv2'>
			<td class='col3'  nowrap='nowrap' style=' width:34%; '>R&eacute;gion ".$aRegion['Nom_region']."</td>
			<td class='col3' align='center'  style=' width:32%; '>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
			<td class='col3' align='center'  style=' width:32%; '>".$sVal."</td>
			</tr>";
		}
		if($sTotalRegion == '')
			$sTotalRegion .= "<tr id='niv2'>
			<td class='col3'  nowrap='nowrap' style=' width:34%; '>R&eacute;gion ".$aRegion['Nom_region']."</td>
			<td class='col3' align='center' style=' width:32%; ' >-</td>
			<td class='col3' align='center' style=' width:32%; ' >0</td>
			</tr>";
		$aSinistreDecRegion = array();
		
		if(empty($aListesAgents)) echo $sSepareTableVide.$sTabSinistreDec;
		else echo $sSepareTable.$sTabSinistreDec;
		
	}
	else if (isset($sTabSinistreReg) && $sTabSinistreReg != "")
	{
		$sTotalRegion = '';
		foreach ($aSinistreRegRegion as $iKey => $sVal)
		{
			foreach ($aPrestationRegion[$iKey] as $iKey1 => $sVal1)
			{
				$sTotalRegion .= "
				<tr id='niv2'>
					<td class='col1' nowrap='nowrap' style=' width:18%;'>R&eacute;gion ".$aRegion['Nom_region']."</td>
					<td class='col2' align='center'  style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
					<td class='col3' align='center'  style=' width:21%;'>".$sVal."</td>
					<td class='col4' align='center'  style=' width:22%;'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1,dataObject_dictionnaire::TYPE_PRESTATION )."</td>
					<td class='col5' align='center'  style=' width:17%;'>".$sVal1."</td>
				</tr>";
				$iMontPrestRegion += $sVal1;
			}
		}
		if($sTotalRegion == '')
			$sTotalRegion .= "
				<tr id='niv2'>
					<td class='col1' nowrap='nowrap' style=' width:18%;'>R&eacute;gion ".$aRegion['Nom_region']."</td>
					<td class='col2' align='center'  style=' width:20%;'>-</td>
					<td class='col3' align='center'  style=' width:21%;'>0</td>
					<td class='col4' align='center'  style=' width:22%;'>-</td>
					<td class='col5' align='center'  style=' width:17%;'>0</td>
				</tr>";
		$aSinistreRegRegion = array();
		$aPrestationRegion = array();
		echo $sSepareTable.$sTabSinistreReg;
	}
	else
	{
		$sTotalRegion = "
		<tr id='niv2'>
			<td  class='col1' nowrap='nowrap' style=' width:24%;'>R&eacute;gion ".$aRegion['Nom_region']."</td>
			<td  class='col2' align='center' style=' width:26%;'>".$iNbContratRegion."</td>
			<td  class='col3' align='center' style=' width:28%;'>".$fCotisationRegion."</td>
			<td  class='col4' align='center' style=' width:20%;'>".$fDroitRegion."</td>
		</tr>";
		$fTotalCotisationRegion =  $iNbContratRegion = $fTotalDERegion = 0;
		$fCotisationRegion = $fDroitRegion = 0;
		echo $sSepareTable.$sTabSous;
	}
	echo  $sTotalRegion;
	if(empty($aListesAgents)) echo '</table>';
	
	
	} // fin foreach parcours des régions
	
	//on affiche finalement le total général pour le siege
	if (isset($sTabActe) && $sTabActe != "")
	{
		$sTotalGeneral = '';
		
		foreach ($aActeGeneral as $iKey => $sVal)
		{
			$sTotalGeneral .= "
			<tr id='niv2'>
				<td class='col3' style=' width:34%; '>Total g&eacute;n&eacute;ral</td>
				<td class='col3' style=' width:32%; ' align='center'>".$iKey."</td>
				<td class='col3' style=' width:32%; ' align='center'>".$sVal."</td>
			</tr>";
		}
		$aActeGeneral = array();
		
	}
	else if (isset($sTabSinistreDec) && $sTabSinistreDec != "")
	{
		$sTotalGeneral = '';
		foreach ($aSinistreDecGeneral as $iKey => $sVal)
		{
			$sTotalGeneral .= "
			<tr id='niv2'>
				<td class='col3' style=' width:34%; '>Total g&eacute;n&eacute;ral</td>
				<td class='col3' align='center'  style=' width:32%; '>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
				<td class='col3' align='center'  style=' width:32%; '>".$sVal."</td>
			</tr>";
		}
		$aSinistreDecGeneral = array();
	}
	else if (isset($sTabSinistreReg) && $sTabSinistreReg != "")
	{
		$sTotalGeneral = '';
		foreach ($aSinistreRegGeneral as $iKey => $sVal)
		{
			foreach ($aPrestationGeneral[$iKey] as $iKey1 => $sVal1)
			{
				$sTotalGeneral .= "
				<tr id='niv2'>
					<td class='col1' nowrap='nowrap' style=' width:18%;'>Total g&eacute;n&eacute;ral</td>
					<td class='col2' align='center'  style=' width:20%;'>".$oElementDico->elementdico_getNomFromCode($iKey, dataObject_dictionnaire::TYPE_SINISTRE)."</td>
					<td class='col3' align='center' style=' width:21%;' >".$sVal."</td>
					<td class='col4' align='center'  style=' width:22%;'>".$oElementDico->elementdico_getNomFromCode('N'.$iKey1,dataObject_dictionnaire::TYPE_PRESTATION )."</td>
					<td class='col5' align='center'  style=' width:17%;'>".$sVal1."</td>
				</tr>";
				$iMontPrestGeneral += $sVal1;
			}
		}
		$aSinistreRegGeneral = array();
		$aPrestationGeneral = array();
	}
	else
	{
		$sTotalGeneral = "
		<tr id='niv2'>
			<td  class='col1' style=' width:24%;'>Total g&eacute;n&eacute;ral</td>
			<td  class='col2' align='center' style=' width:26%;'>".$iNbContratGeneral."</td>
			<td  class='col3' align='center' style=' width:28%;'>".$fCotisationGeneral."</td>
			<td  class='col4' align='center' style=' width:20%;'>".$fDroitGeneral."</td>
		</tr>";
		
	}
	if($bShowTotalGeneral) echo $sTotalGeneral;	 

	for($i=1;$i<=$iNbTableHtml;$i++) 
			{
			?>
				</table>
			<?php
			}	
	
}


?>

				 </div> <!-- fin content_table -->   
				 </PDF>
			</div>
			
			<div class="imgright1"></div>			
			<div class="cleared"></div> 
			
			
			<?php

	$sHtml = ob_get_contents();
	//$sNomListe = utf8_decode($sNomListe);
	$sNomfichier = preg_replace('/&eacute;/','e',$sNomListe);
	$sNomfichier = preg_replace('/&egrave;/','e',$sNomfichier);
	$sNomfichier = preg_replace('/&#130;/','_',$sNomfichier);
	$sNomfichier = str_replace("d'",'',$sNomfichier);
	$sNomfichier = str_replace("é",'e',$sNomfichier);
	$oFichierPDF = new CHMTL_PDF($sHtml, $sNomfichier,'L',false, false);
	$sUrlPrint = "succes_impression.php?file=".$oFichierPDF->sFileEdition;
	?>
			<form action="<?php echo $sEditFormAction ?>" method="post" name="form1" id="form1" class="formulaire">
				<input type="hidden" name="nomListe" maxlength="50" class="resizeall" value="<?php echo $sNomListe ?>" size="32" />
				<table>
					<tr>
						<td class='col15' align="center">&nbsp;&nbsp;ADRESSE MAIL</td>
						<td ><input type="text" name="OEM_email" maxlength="50" class="resizeall" value="" size="32" /></td>
					</tr>
				</table>
			
				<input type="hidden" name="file" value="<?php echo $oFichierPDF->sEdition; ?>" />
				<input type="hidden" name="deb" value="<?php echo formaterDate($sDateDebut,"-","/",false); ?>" />
				<input type="hidden" name="fin" value="<?php echo formaterDate($sDateFin,"-","/",false); ?>" />
				
				<div class="bt1">
					<span class="btleft"></span> <a href="../editions"> 
					<input type="submit" name="Retour" value="Retour" class="resizeall btmiddle" />
					<span class="btright"></span> </a>
				</div>
				<div class="bt1">
					<span class="btleft"></span> 
					<input type="submit" name="Envoyer" value="Envoyer par mail" class="resizeall btmiddle" /> 
					<span class="btright"></span>
				</div>
			</form>
			<div class="bt1">
				<span class="btleft"></span> 
				<input type="submit" name="Imprimer" value="Imprimer" class="resizeall btmiddle" onclick="javascript:printEdition('<?php echo $sUrlPrint ?>');" />
				<span class="btright"></span>
			</div>
			
			<!--/***************************************/-->
		</div>
	</body>
</html>
	
		<?php
		include_once PATH_INCLUDE.'inc_pied_page.php';

/**
 * fonction qui permet de changer le format d'une date 
 * 
 * @param  string $sDate date à formater : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 * @param  char $sSeparateur sépareteur de la date à formater : vaut "-" ou "/"
 * @param  char $NouveauSeparateur sépareteur de la date formater (resultat) : vaut "-" ou "/"
 * @param  bool $bInverser : vaut true si on veut inveser le format de la date etré en parametre et false sinon
 * @return string : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 */
function formaterDate($sDate, $sSeparateur = "-", $NouveauSeparateur = "-", $bInverser = true)
{
	$aTab = explode($sSeparateur, $sDate);
	
	if ($bInverser)
		$sRetour = $aTab[2].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[0];
	else
		$sRetour = $aTab[0].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[2];
	
	return $sRetour;
}
		?>